package mis.ds.ps.ui.swing;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import mis.ds.ps.model.Mapper;
import mis.ds.ps.model.MapperComposite;
import mis.ds.ps.model.MapperLeaf;

/**
 * 
 * @author edjesus
 */
public class MapperTree {

	Map<String, Map<String, AVP>> entities;

	public MapperTree(Mapper mapper) {

		entities = new HashMap<String, Map<String, AVP>>();
		AVP avp = new AVP("No mapping", 0);

		// =============================CURSOS============================
		Map<String, AVP> cursosMap = new HashMap<String, MapperTree.AVP>();
		cursosMap.put("codigo", avp);
		cursosMap.put("sigla", avp);
		cursosMap.put("nome", avp);
		entities.put("Cursos", cursosMap);
		// ================================= =============================

		// =============================DISCIPLINAS============================
		Map<String, AVP> disciplinasMap = new HashMap<String, MapperTree.AVP>();
		disciplinasMap.put("codigo", avp);
		disciplinasMap.put("sigla", avp);
		disciplinasMap.put("nome", avp);
		entities.put("Disciplinas", disciplinasMap);
		// ================================= =============================

		// =============================DOCENTES============================
		Map<String, AVP> docentesMap = new HashMap<String, MapperTree.AVP>();
		docentesMap.put("codigo", avp);
		docentesMap.put("sigla", avp);
		docentesMap.put("nome", avp);
		docentesMap.put("email", avp);
		docentesMap.put("telefone", avp);
		entities.put("Docentes", docentesMap);
		// ================================= =============================

		// =============================MODULOS============================
		Map<String, AVP> modulosMap = new HashMap<String, MapperTree.AVP>();
		modulosMap.put("codigo", avp);
		modulosMap.put("inicio", avp);
		modulosMap.put("fim", avp);
		modulosMap.put("nome", avp);
		entities.put("Modulos", modulosMap);
		// =============================== =================================

		// =============================SALAS============================
		Map<String, AVP> salasMap = new HashMap<String, MapperTree.AVP>();
		salasMap.put("codigo", avp);
		salasMap.put("sigla", avp);
		entities.put("Salas", salasMap);
		// =============================== =================================

		// =============================TURMAS============================
		Map<String, AVP> turmasMap = new HashMap<String, MapperTree.AVP>();
		turmasMap.put("ordem", avp);
		entities.put("Turmas", turmasMap);
		// =============================== =================================

		MapperComposite mc = (MapperComposite) mapper;
		for (Iterator<Mapper> it = mc.getMappersList().iterator(); it.hasNext();) {

			MapperLeaf ml = (MapperLeaf) it.next();

			if (ml.getEntity().getType().equalsIgnoreCase("disciplinas")) {
				if (ml.getAttribute().equalsIgnoreCase("nome")) {
					AVP nomeAvp = new AVP(ml.getDbStartCell() + ";" + ml.getDbEndCell(), ml.getSheet());
					disciplinasMap.put("nome", nomeAvp);
					entities.put("Disciplinas", disciplinasMap);
				} else if (ml.getAttribute().equalsIgnoreCase("sigla")) {
					AVP siglaAvp = new AVP(ml.getDbStartCell() + ";" + ml.getDbEndCell(), ml.getSheet());
					disciplinasMap.put("nome", siglaAvp);
					entities.put("Disciplinas", disciplinasMap);
				} else if (ml.getAttribute().equalsIgnoreCase("codigo")) {
					AVP codigoAvp = new AVP(ml.getDbStartCell() + ";" + ml.getDbEndCell(), ml.getSheet());
					disciplinasMap.put("nome", codigoAvp);
					entities.put("Disciplinas", disciplinasMap);
				}
			}else if(ml.getEntity().getType().equalsIgnoreCase("cursos")){
				if (ml.getAttribute().equalsIgnoreCase("nome")) {
					AVP nomeAvp = new AVP(ml.getDbStartCell() + ";" + ml.getDbEndCell(), ml.getSheet());
					cursosMap.put("nome", nomeAvp);
					entities.put("Cursos", cursosMap);
				} else if (ml.getAttribute().equalsIgnoreCase("sigla")) {
					AVP siglaAvp = new AVP(ml.getDbStartCell() + ";" + ml.getDbEndCell(), ml.getSheet());
					cursosMap.put("nome", siglaAvp);
					entities.put("Cursos", cursosMap);
				} else if (ml.getAttribute().equalsIgnoreCase("codigo")) {
					AVP codigoAvp = new AVP(ml.getDbStartCell() + ";" + ml.getDbEndCell(), ml.getSheet());
					cursosMap.put("nome", codigoAvp);
					entities.put("Cursos", cursosMap);
				}
			}
		}

	}

	public Map<String, Map<String, AVP>> getEntities() {
		return entities;
	}

	// Represents avp for entity attribute
	public class AVP {
		private Object value;
		private int sheetIndex;

		public AVP(Object value, int sheetIndex) {
			super();
			this.value = value;
			this.sheetIndex = sheetIndex;
		}

		public Object getValue() {
			return value;
		}

		public int getSheetIndex() {
			return sheetIndex;
		}

		public void setSheetIndex(int sheetIndex) {
			this.sheetIndex = sheetIndex;
		}

		public void setValue(Object value) {
			this.value = value;
		}

		@Override
		public String toString() {
			return "-" + value + "-" + sheetIndex;
		}

	}
}
